package cn.iocoder.yudao.module.member.controller.admin.userproblem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userproblem.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userproblem.UserProblemDO;
import cn.iocoder.yudao.module.member.service.userproblem.UserProblemService;

@Tag(name = "管理后台 - 用户做过的问题")
@RestController
@RequestMapping("/member/user-problem")
@Validated
public class UserProblemController {

    @Resource
    private UserProblemService userProblemService;

    @PostMapping("/create")
    @Operation(summary = "创建用户做过的问题")
    @PreAuthorize("@ss.hasPermission('member:user-problem:create')")
    public CommonResult<Long> createUserProblem(@Valid @RequestBody UserProblemSaveReqVO createReqVO) {
        return success(userProblemService.createUserProblem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户做过的问题")
    @PreAuthorize("@ss.hasPermission('member:user-problem:update')")
    public CommonResult<Boolean> updateUserProblem(@Valid @RequestBody UserProblemSaveReqVO updateReqVO) {
        userProblemService.updateUserProblem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户做过的问题")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-problem:delete')")
    public CommonResult<Boolean> deleteUserProblem(@RequestParam("id") Long id) {
        userProblemService.deleteUserProblem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户做过的问题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-problem:query')")
    public CommonResult<UserProblemRespVO> getUserProblem(@RequestParam("id") Long id) {
        UserProblemDO userProblem = userProblemService.getUserProblem(id);
        return success(BeanUtils.toBean(userProblem, UserProblemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户做过的问题分页")
    @PreAuthorize("@ss.hasPermission('member:user-problem:query')")
    public CommonResult<PageResult<UserProblemRespVO>> getUserProblemPage(@Valid UserProblemPageReqVO pageReqVO) {
        PageResult<UserProblemDO> pageResult = userProblemService.getUserProblemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserProblemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户做过的问题 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-problem:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserProblemExcel(@Valid UserProblemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserProblemDO> list = userProblemService.getUserProblemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户做过的问题.xls", "数据", UserProblemRespVO.class,
                        BeanUtils.toBean(list, UserProblemRespVO.class));
    }

}